<?php
/**
 * @author Matteo Vigoni <mattevigo@gmail.com>
 * @package PHPorcupine 2.0
 *
 * Class Relase
 */
require_once("DBEntity.php");

// Social Interfaces //////////////////////////////////////////////////////////////////////////
require_once 'social/FacebookInterface.php';
require_once 'social/TwitterInterface.php';

// Class Definition ///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
class Relase extends DBEntity implements FacebookInterface, TwitterInterface
{
	// tabella
	const TABLE = 'relases';
	const PRIMARY_KEY = 'relase_id';

	const AUTH_GROUP = "releases";

	/**
	 * Costruttore
	 *
	 * @param $db
	 * @param $id
	 * @return unknown_type
	 */
	public function __construct(DB $db, $id=NULL, $table=NULL, $primary_key=NULL )
	{
		parent::__construct($db, self::TABLE, self::PRIMARY_KEY, $id);
	}

	// Actions ///////////////////////////////////////////////////////////////////////////
	
	/**
	 * Check the input array
	 * 
	 * @param array $input
	 * @param $err the var where to write the error message
	 * @return true if the array is valid
	 * 			false otherwise (set $err)
	 */
	public function checkInput( $input, $err )
	{	
		if( !isset( $input['release_title'] ) || strcmp( $input['release_title'], "" ) == 0 )
		{
			$err = "please insert a title for this release";
			return false;
		}
		
		if( !isset( $input['release_date'] ) || strcmp( $input['release_date'], "") == 0 )
		{
			$err = "please specified an issue date";
			return false;
		}
		
		return true;
	}

	/**
	 *
	 * @param User $user
	 * @param Session $session
	 * @return void
	 * @throws 	DBEntityException if user don't the rights or not a user specified
	 * 			
	 */
	public function doPost( User $user=NULL, Session $session=NULL )
	{
		global $_POST;
		$err = "";

		// User ////////////////////////////////////////////////////////////////////////
		$usr = NULL;
		if( $user == NULL )
		{
			if( $this->getUser() != NULL )
			{
				$usr = $this->getUser();

				if( !$usr->isInGroup( self::AUTH_GROUP ))
				{
					throw new DBEntityException("You don't have the rights");
				}
			}
			else
			{
				throw new DBEntityException("No User specified");
			}
		}
		else
		{
			$usr = $user;
		}

		$this->set('user_id', $usr->getId());	# set user_id in the object

		// SETS /////////////////////////////////////////////////////////////////////////
		if( $this->checkInput( &$_POST, &$err ) )
		{		
			$this->set('release_title', $_POST['release_title']);
			$this->set('release_description', $_POST['release_description']);
			$this->set('release_date', $_POST['release_date']);
			
			$this->set('release_link', $_POST['release_link']);
			$this->set('release_youtube', $_POST['release_youtube']);
			$this->set('release_torrent', $_POST['release_torrent']);
			$this->set('release_ed2k', $_POST['release_ed2k']);
		}
		else
		{
			throw new InvalidInputException( "Input check not passed: $err" );
		}
	}

	// SOCIAL ///////////////////////////////////////////////////////////////////////////////////

	///////// Facebook Functions ///////////

	public function setFacebook( Facebook $f )
	{
		$this->set('facebook', $f);
	}

	public function updateStatus( User $usr )
	{
		//throw new NotImplementedException("function updateStatus() not implemented");
		$this->get('facebook');
	}

	////////// Twitter Functions ///////////

	public function getTwitterEmail()
	{
		throw new NotImplementedException("function getTwitterEmail() not implemented");
	}

	public function getTwitterUsername()
	{
		throw new NotImplementedException("function getTwitterUsername() not implemented");
	}

	public function getTwitterPassword()
	{
		throw new NotImplementedException("function getTwitterPassword() not implemented");
	}

	public function tweet( User $usr )
	{
		throw new NotImplementedException("function tweet() not implemented");
	}
}